package com.hjtm.easypigfarm.widget;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.hjtm.easypigfarm.R;
import com.hjtm.easypigfarm.fragment.FragmentAbout;
import com.hjtm.easypigfarm.fragment.FragmentCostEarn;
import com.hjtm.easypigfarm.fragment.FragmentPigStore;
import com.hjtm.easypigfarm.fragment.FragmentReminder;
import com.hjtm.easypigfarm.fragment.FragmentSetting;
import com.hjtm.easypigfarm.utility.Constants;
import com.hjtm.easypigfarm.utility.PigUtils;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainTabActivity extends FragmentActivity {

	private final String TAG = MainTabActivity.class.getSimpleName();
	public static String sFarmSelect = null;
	// private Fragment contentFragment;
	private RadioGroup myTabRg;
	private FragmentPigStore pigStore; // PigFarm store
	private FragmentCostEarn costEarn; // Cost and earn
	private FragmentReminder reminder; // Management reminder
	private FragmentSetting setting; // Choose a pig farm
	private FragmentAbout about; // About us
	private TextView mFarmName;
	private Button mDate;
	private Handler mHandler;
	private PigUtils mPigUtil;
	private Context mContext;
	private DatePicker mDatePicker;

	private int year;
	private int month;
	private int day;

	private String mSelectDate;

	// private ArrayList<String> mFarms= new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabs);
		mContext = this;
		mHandler = new Handler() {

			public void handleMessage(Message msg) {
				switch (msg.what) {
				case Constants.MSG_UPDATE_FARMS:
					// populate farms
					ArrayList<String> farm = (ArrayList<String>) msg.obj;
					if (farm != null && !farm.isEmpty()) {
						setting.updateFarms(farm);
					}
					break;
				default:
					break;
				}
			}
		};

		// get provinces
		mPigUtil = new PigUtils(mHandler);
		initView();
	}

	public void setFarmSelect(String farm) {
		sFarmSelect = farm;
		if (mFarmName != null) {
			mFarmName.setText(sFarmSelect);
			mFarmName.setVisibility(View.VISIBLE);
		}
	}

	public void initView() {
		mFarmName = (TextView) findViewById(R.id.farmName);
		if (sFarmSelect == null) {
			mFarmName.setVisibility(View.GONE);
		} else {
			mFarmName.setText(sFarmSelect);
		}
		mDate = (Button) findViewById(R.id.date);
		mDate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DatePickerDialog datePicker = new DatePickerDialog(
						MainTabActivity.this, new OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								// TODO Auto-generated method stub
								Toast.makeText(
										MainTabActivity.this,
										year + "year " + (monthOfYear + 1)
												+ "month " + dayOfMonth + "day",
										Toast.LENGTH_SHORT).show();
								String date = year + "/" + monthOfYear + "/"
										+ dayOfMonth + "/";
								mDate.setText(date);
							}
						}, year, month, day);
				datePicker.show();

			}
		});
		mDatePicker = (DatePicker) findViewById(R.id.datePicker1);

		/*
		 * mDatePicker.init(2016, 5, 1, new OnDateChangedListener() {
		 * 
		 * @Override public void onDateChanged(DatePicker view, int year, int
		 * monthOfYear, int dayOfMonth) { // 获取一个日历对象，并初始化为当前选中的时间 Calendar
		 * calendar = Calendar.getInstance(); calendar.set(year, monthOfYear,
		 * dayOfMonth); SimpleDateFormat format = new SimpleDateFormat(
		 * "yyyy年MM月dd日  HH:mm"); Toast.makeText(mContext,
		 * format.format(calendar.getTime()), Toast.LENGTH_SHORT) .show(); } });
		 */

		// mDatePicker.setMaxDate(year)

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new java.util.Date());
		Date mdate = new Date();
		if (date != null) {
			mSelectDate = date;
			Calendar mcalendar = Calendar.getInstance(Locale.CHINA);
			mcalendar.setTime(mdate);
			year = mcalendar.get(Calendar.YEAR); // 获取Calendar对象中的年
			month = mcalendar.get(Calendar.MONTH);// 获取Calendar对象中的月
			day = mcalendar.get(Calendar.DAY_OF_MONTH);// 获取这个月的第几天
			mDate.setText(date);
		}
		mDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(mContext, "set date", Toast.LENGTH_SHORT).show();

				/* Dialog dialog = null; */

				// Calendar c = Calendar.getInstance();
				/*
				 * Calendar mycalendar = Calendar.getInstance(Locale.CHINA);
				 * Date mydate = new Date(); // 获取当前日期Date对象
				 * mycalendar.setTime(mydate);// //为Calendar对象设置时间为当前日期
				 */
				/*
				 * year = mycalendar.get(Calendar.YEAR); // 获取Calendar对象中的年
				 * month = mycalendar.get(Calendar.MONTH);// 获取Calendar对象中的月 day
				 * = mycalendar.get(Calendar.DAY_OF_MONTH);// 获取这个月的第几天
				 */DatePickerDialog dpd = new DatePickerDialog(mContext,
						new DatePickerDialog.OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view,
									int thisyear, int monthOfYear,
									int dayOfMonth) {
								year = thisyear;
								month = monthOfYear;
								day = dayOfMonth;
								// 更新日期
								updateDate();
							}

							private void updateDate() {
								// 在TextView上显示日期
								mSelectDate = year + "-" + (month + 1) + "-"
										+ day;
								mDate.setText(mSelectDate);
							}

						}, year, month, day);
				dpd.show();
			}
		});
		setting = new FragmentSetting();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_content, setting).commit();
		myTabRg = (RadioGroup) findViewById(R.id.tab_menu);
		myTabRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rbChat:
					pigStore = new FragmentPigStore(); // PigFarm store
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.main_content, pigStore).commit();
					break;
				case R.id.rbAddress: // Cost and earn
					costEarn = new FragmentCostEarn();
					Log.i(TAG, "FragmentAddress");
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.main_content, costEarn).commit();
					break;
				case R.id.rbFind:
					reminder = new FragmentReminder(); // Managerment
														// reminder
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.main_content, reminder).commit();
					break;
				case R.id.rbMe:
					if (setting == null) { // Choose a pigfarm
						setting = new FragmentSetting();
					}
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.main_content, setting).commit();
					break;
				case R.id.rbAbout:
					about = new FragmentAbout(); // About us
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.main_content, about).commit();
				default:
					break;
				}

			}
		});
	}

	@Override
	public void onBackPressed() {
		if (isFinishing()) {
			return;
		}
		new AlertDialog.Builder(this).setTitle(R.string.exit_reminder)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setPositiveButton(R.string.enter, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 点击“确认”后的操作
						MainTabActivity.this.finish();
					}
				})
				.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 点击“返回”后的操作,这里不设置没有任何操作
					}
				}).show();
	}

}
